package com.cpj.bsyyzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpj.bsyyzx.entity.AccountRole;
import com.cpj.bsyyzx.entity.Permission;
import com.cpj.bsyyzx.entity.RolePermission;
import com.cpj.bsyyzx.mapper.PermissionMapper;
import com.cpj.bsyyzx.service.IAccountRoleService;
import com.cpj.bsyyzx.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cpj.bsyyzx.service.IRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 五个臭皮匠
 * @since 2021-05-09
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    IAccountRoleService accountRoleService;

    @Autowired
    IRolePermissionService rolePermissionService;

    @Override
    public Page pagelist(Page page,Permission permission) {
        if (page==null){
            page=new Page();
        }
        QueryWrapper wrapper=new QueryWrapper();
        if (permission!=null&& !StringUtils.isEmpty(permission.getName())){
            wrapper.like("name",permission.getName());
        }
        return super.page(page,wrapper);
    }

    @Override
    public Object list(Permission permission,Page page1) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        IPage<Permission> page = this.page(page1,wrapper);
        List<Permission> list = page.getRecords();
        for(Permission permission1 : list) {
            getChildren(permission1);
        }
        return page;
    }

    @Override
    public boolean add(Permission permission) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("name",permission.getName());
        Permission query = this.getOne(wrapper);
        if(query == null )
            return this.save(permission);
        return false;
    }

    private void getChildren(Permission permission) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",permission.getPermissionId());
        List<Permission> list = this.list(wrapper);
        for(Permission permission1 : list) {
            getChildren(permission1);
        }
        permission.setChildren(list);
    }

    @Override
    public List<Permission> userPermissionList(Integer accId) {
        //1根据用户id查询出所有的角色ums_user_role
        QueryWrapper<AccountRole> umsUserQueryWrapper = new QueryWrapper<>();
        umsUserQueryWrapper.eq("acc_id",accId);
        AccountRole accountRole = accountRoleService.getOne(umsUserQueryWrapper);
        if(accountRole==null || accountRole.getRoleId() == null){
            return null;
        }

        //System.out.println(umsUserRole.getRoleId()); //
        String[] roleIds = accountRole.getRoleId().split(",");
        //根据角色获取权限id集合
        QueryWrapper<RolePermission> umsRolePermissionWrapper = new QueryWrapper<>();
        umsRolePermissionWrapper.in("role_id",roleIds);
        List<RolePermission> rolePermissionList = rolePermissionService.list(umsRolePermissionWrapper);
        if(rolePermissionList ==null ){
            return null;
        }

        Set permissionIdSet = new HashSet();
        for (RolePermission umsRolePermission : rolePermissionList) {
            permissionIdSet.addAll(Arrays.asList(umsRolePermission.getPermissionId().split(",")));
        }

//        permissionIdSet.forEach(System.out::println);

        //根据权限id查询权限列表
        QueryWrapper<Permission> umsPermissionQueryWrapper = new QueryWrapper<>();
        umsPermissionQueryWrapper.in("permission_id",permissionIdSet);
        List<Permission> allPermission = this.list(umsPermissionQueryWrapper);
        //目标，将顶层菜单放入
        List<Permission> frontPermissionList = new ArrayList();
        for (Permission permission : allPermission) {
            if(permission.getParentId() == 0){
                setChildrenPermission(permission,allPermission);
                frontPermissionList.add(permission);
            }
        }
        return frontPermissionList;
    }

    private void setChildrenPermission(Permission permission, List<Permission> allPermission) {
        List<Permission> children = new ArrayList();
        for (Permission umsPermission : allPermission) {
            if(permission.getPermissionId() == umsPermission.getParentId()){
                setChildrenPermission(umsPermission,allPermission);
                children.add(umsPermission);
            }
        }
        permission.setChildren(children);
    }

    @Override
    public List<Permission> userPermissionListValidate(Integer accId) {
        //1根据用户id查询出所有的角色ums_user_role
        QueryWrapper<AccountRole> umsUserQueryWrapper = new QueryWrapper<>();
        umsUserQueryWrapper.eq("acc_id",accId);
        AccountRole accountRole = accountRoleService.getOne(umsUserQueryWrapper);
        if(accountRole==null || accountRole.getRoleId() == null){
            return null;
        }

        //System.out.println(umsUserRole.getRoleId()); //
        String[] roleIds = accountRole.getRoleId().split(",");
        //根据角色获取权限id集合
        QueryWrapper<RolePermission> umsRolePermissionWrapper = new QueryWrapper<>();
        umsRolePermissionWrapper.in("role_id",roleIds);
        List<RolePermission> rolePermissionList = rolePermissionService.list(umsRolePermissionWrapper);
        if(rolePermissionList ==null ){
            return null;
        }

        Set permissionIdSet = new HashSet();
        for (RolePermission umsRolePermission : rolePermissionList) {
            permissionIdSet.addAll(Arrays.asList(umsRolePermission.getPermissionId().split(",")));
        }

        //根据权限id查询权限列表
        QueryWrapper<Permission> umsPermissionQueryWrapper = new QueryWrapper<>();
        umsPermissionQueryWrapper.in("acc_id",permissionIdSet);
        List<Permission> allPermission = this.list(umsPermissionQueryWrapper);
        return allPermission;
    }
}
